﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;

public partial class themsanphamvaogio : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataConnection.str = @"Data Source=xe;User ID=ODER;Password=12345";
        if (!IsPostBack)
        {
            if (Session["TenDN"] != null)
            {
                if (Request.QueryString["MaSach"] != null)
                {
                    ThemSPVaoGioHang();
                    Response.Redirect(Request.UrlReferrer.ToString());
                }
                else
                {
                    Response.Redirect("thongbaoloi.aspx?ID=2");
                }
            }
            else
            {
                Response.Redirect("thongbaoloi.aspx?ID=3");
            }
        }

    }
    private void ThemSPVaoGioHang()
    {
        string masach = Request.QueryString["MaSach"].ToString();

        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_masach",masach),
           
        };
        // get Sach By MaSach
        DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("NGOCTRAN.sp_getSachByMaSach", thamso);
        string tensach = dt.Rows[0][1].ToString();
        int dongia = int.Parse(dt.Rows[0][5].ToString());
        string hinh = "image/" + dt.Rows[0][7].ToString();
        DataTable gioHang = (DataTable)Session["GioHang"];
        // Kiem tra san pham da ton tai trong gio hang hay chua?
        int kt = 0;
        for (int i = 0; i < gioHang.Rows.Count; i++)
        {   
            string temp = gioHang.Rows[i][0].ToString();
            if (temp == masach)
            {
                kt = 1;
                int slHienTai = int.Parse(gioHang.Rows[i]["soluong"].ToString());
                gioHang.Rows[i]["soluong"] = slHienTai + 1;
                break;
            }
        }
        if (kt == 0)
        {
            DataRow dr = gioHang.NewRow();
            dr["masach"] = masach;
            dr["hinh"] = hinh;
            dr["tensach"] = tensach;
            dr["dongia"] = dongia;
            dr["soluong"] = 1;
            gioHang.Rows.Add(dr);
            Session["GioHang"] = gioHang;
            Session["MaSach"] = masach;
        }
        Response.Redirect("thongbaomuahang.aspx");
    }
}